<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['credit_types']=$this->credit_types;
if($_RQ['op']=='main'){
	set_html('title','我的发布');
	$return['params']['type']=1;
	$return['list']=array();
	exi($return);
}
if($_RQ['op']=='getlist'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	$params['type']=intval($_RQ['type'])?:1;
	$return['params']['type']=intval($_RQ['type'])?:1;	
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);	
	}
	if($_RQ['keyword']){
		if(is_numeric($_RQ['keyword'])){
			$params['id']=intval($_RQ['keyword']);
		}else{
			$params['title like']='%'.$_RQ['keyword'].'%';
		}
		$return['params']['keyword']=$_RQ['keyword'];		
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_creditchange_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$kv){
			$kv['image']=$kv['image']?tomedia($kv['image']):'';
	}	
	if($list){
		foreach($list as &$item){
			$item['updatetime']=$item['updatetime']?:$item['createtime'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$member=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('nickname','avatar'));
			$item['avatar']=$item['avatar']?$item['avatar']:$member['avatar'];
			$item['image']=tomedia($item['image']);
			$lasttime=TIMESTAMP-$item['updatetime'];
			if($lasttime>24*3600){
				$item['updatetimetitle']=intval($lasttime/(24*3600)).'天前';
			}elseif($lasttime>=3600){
				$item['updatetimetitle']=intval($lasttime/3600).'小时前';
			}else{
				$item['updatetimetitle']=intval($lasttime/60).'分钟前';
			}
			$item['statustitle']=$this->goods_status[$item['status']];
			$orders=pdo_getall('jy_creditchange_order',array('goodsid'=>$item['id'],'ispay'=>1),array('id','status','mid','fee','createtime'));
			$item['usernum']=count($orders);
			foreach($orders as &$ov){
				$ov['member']=pdo_get('core_members',array('id'=>$ov['mid'],'uniacid'=>$_SESSION['uniacid']),array('nickname','avatar'));
				$ov['createtime']=date('Y/m/d H:i:s',$ov['createtime']);
				$ov['statustitle']=$this->order_status[$ov['status']];
			}
			$item['orders']=$orders;
			$item['goufee']=$item['creditnum']*$return['pluginset']['gourate'.$item['type']]/100;
		}
	}
	$return['list']=$list;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_creditchange_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if($item['status'] > 1){
			exi('该转让已经结束，不能编辑！','error');
		}
		$orders=pdo_count('jy_creditchange_order',array('goodsid'=>$id,'ispay'=>1));
		if($orders){
			exi('已经有人竞价了，不能编辑！','error');
		}
	}else{
		$item=array('type'=>1);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'main','title'=>'返回转让列表'),
		),
		'edit'=>array(
					array('name'=>'title','title'=>'转让名称','type'=>'string'),
					array('name'=>'image','title'=>'转让封面','type'=>'image'),
					//array('type'=>'choose','title'=>'选择发布人','name'=>'mid','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
					array('name'=>'desc','title'=>'转让介绍','type'=>'text'),
					array('name'=>'type','title'=>'转让虚拟币类型','type'=>'radio','options'=>'credit_types'),
					array('name'=>'creditnum','title'=>'转让数量','type'=>'string'),
					array('name'=>'price','title'=>'转让价格','type'=>'string'),
					//array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['type']){
		exi('请选择转让类型！','error');
	}
	if(!$postdata['title']){
		exi('请填写转让名称！','error');
	}
	if(!$postdata['creditnum']){
		exi('请上传转让数量！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'type'=>intval($postdata['type']),
		'desc'=>trim($postdata['desc']),
		'price'=>trim($postdata['price']),
		'status'=>1,
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_creditchange_goods',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=TIMESTAMP;
		$data['creditnum']=intval($postdata['creditnum']);
		$this->model('member')->recharge($data['mid'],$data['creditnum'],$data['type']);
		pdo_insert('jy_creditchange_goods',$data);
	}
	exi('操作成功！','','main');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择转让！','error');
	}
	$this->model('model')->dacheng($id);
	exi('操作成功！可到账您的【现金红包】');
}
if($_RQ['op']=='gou'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择转让！','error');
	}
	$this->model('model')->setgou($id);
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	if($_RQ['id']){
		$id=$_RQ['id'];
	}
	$orders=pdo_count('jy_creditchange_order',array('goodsid'=>$id,'ispay'=>1));
	if($orders){
		exi('已经有人竞价了，不能删除！','error');
	}
	//出让方解冻消耗
	$goods=pdo_get('jy_creditchange_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$this->model('member')->recharge($goods['mid'],$goods['creditnum'],$goods['type'],2);
	pdo_delete('jy_creditchange_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('删除成功！');
}